﻿'
' Copyright (c) Microsoft Corporation. All rights reserved.
'
' This source code is subject to terms and conditions of the Microsoft Public
' License (Ms-PL). A copy of the license can be found in the license.htm file
' included in this distribution.
'
' You must not remove this notice, or any other, from this software.
'
Public MustInherit Class VbView
    Implements IView, IViewDataContainer

    Private _viewContext As ViewContext = Nothing
    Private _viewData As ViewDataDictionary = Nothing
    Private _xhtmlHelper As XhtmlHelper = Nothing

    Public MustOverride Function Render() As XElement

    Friend Function Render(ByVal viewContext As ViewContext) As XElement
        Try
            _viewContext = viewContext
            SetViewData(viewContext.ViewData)
            _xhtmlHelper = New XhtmlHelper(viewContext, Me)

            Return Render()
        Finally
            _viewContext = Nothing
            _viewData = Nothing
            _xhtmlHelper = Nothing
        End Try
    End Function

    Public Sub Render(ByVal viewContext As ViewContext, ByVal writer As TextWriter) Implements IView.Render
        writer.WriteLine("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"">")

        Dim content = Render(viewContext)
        writer.Write(content.ToString.Replace("<amp />", "&")) ' replacing "<amp />" with "&" is a workaround for work item: 949
    End Sub

    Public Property ViewData() As ViewDataDictionary Implements IViewDataContainer.ViewData
        Get
            If _viewData Is Nothing Then
                SetViewData(New ViewDataDictionary)
            End If
            Return _viewData
        End Get
        Set(ByVal value As ViewDataDictionary)
            SetViewData(value)
        End Set
    End Property

    Public ReadOnly Property Xhtml() As XhtmlHelper
        Get
            Return _xhtmlHelper
        End Get
    End Property

    Public ReadOnly Property Context() As HttpContextBase
        Get
            Return _viewContext.HttpContext
        End Get
    End Property

    Public ReadOnly Property Model() As Object
        Get
            Return _viewData.Model
        End Get
    End Property

    Protected Overridable Sub SetViewData(ByVal ViewData As ViewDataDictionary)
        _viewData = ViewData
    End Sub

End Class

